# This code is part of the MWP System
# Copyright (c) 2012 Benjamin Lubin (blubin@bu.com) 
# Published under and subject to the GPLv2 license available at http://www.gnu.org/licenses/gpl-2.0.html

'''
Created on Dec 13, 2012
@author: blubin
'''

from backend.backend import Backend
from workflow.task import Task
from workflow.result import Result
from workflow.flowData import Status
from random import randrange

class SgBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "storagegalore", dashboard=True);

    def wire(self):
        self.register_result_listener("Client", "SendDocument", self.send_document_complete)
        self.register_result_listener("Scanner", "ScanDocument", self.scan_complete)
        self.register_result_listener("QA", "CheckQuality", self.check_quality_complete)
        self.register_result_listener("Typist1", "CreateIndex", self.index_complete)
        self.register_result_listener("QA", "CompareIndexes", self.compare_indexes_complete)
        self.register_result_listener("QA", "ReconcileIndexes", self.reconcile_indexes_complete)
        self.register_result_listener("Client", "ViewDocument", self.view_document_complete)
         
    def send_document_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Scanner", "ScanDocument");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def scan_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "QA", "CheckQuality");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def check_quality_complete(self, results):
        for result in results:
            
            randomNumber = randrange(1,100)
            print randomNumber
            if randomNumber > 90:
                task = Task.construct_from_result(result, "Scanner", "ScanDocument")
            else:
                task = Task.construct_from_result(result, "Typist1", "CreateIndex")
                
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def index_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "QA", "CompareIndexes");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def compare_indexes_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "QA", "ReconcileIndexes");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def reconcile_indexes_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Client", "ViewDocument");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);
            
    def view_document_complete(self, results):
        for result in results:
            self.workflow.update_status(result, Status.COMPLETE);

if __name__ == '__main__':
    backend = SgBackend();